import { _decorator, AudioClip, Component, director, Node } from 'cc';
const { ccclass, property, executionOrder } = _decorator;

import { SSS_Consts } from "../Data/SSS_Consts";
import { SSS_GameData } from "../Data/SSS_GameData";
import { bdlMgr } from "../Manager/BundleManager_S";
import SSS_GameEventManager from "../Manager/SSS_GameEventManager";
import UI from "../Manager/UI";
import MainPanel from "../UI/MainPanel";
import StartPanel from "../UI/StartPanel";
import { SSS_AudioManager } from '../Manager/SSS_AudioManager';
import { TempSounds } from '../UI/SettingsPanel';

// // 慢一点执行
@ccclass('MainScene')
@executionOrder(101)
export default class MainScene extends Component {
        static Instance: MainScene;
        @property(Node)
        public UI: Node | null = null;
        async onLoad() {
                MainScene.Instance = this;

                //        // {
                //        //     // 获取碰撞检测系统：
                //        //     let manager = cc.director.getCollisionManager();
                //        //     // 默认碰撞检测系统是禁用的，如果需要使用则需要以下方法开启碰撞检测系统：
                //        //     manager.enabled = true;
                //        //     // 默认碰撞检测系统的 debug 绘制是禁用的，如果需要使用则需要以下方法开启 debug 绘制：
                //        //     // manager.enabledDebugDraw = true;
                //        // }

                //        // GameStorage.initData();

                SSS_GameData.InitData();
                SSS_GameData.预加载();


                TempSounds.InitMusic()

                UI.Instance.ResetUINode();

                //        // cc.director.preloadScene("GameScene");

                //        StartPanel.Instance.ShowOn();
                //        MainPanel.Instance.ShowOff();

                StartPanel.Instance.ShowOff();
                MainPanel.Instance.ShowOn();

                if (SSS_GameData.isFirstEnterMainScene) {
                        SSS_GameEventManager.Init();
                        // SSS_GameEventManager.AddListener();

                        let bgm = `bgm/To Mars And Back_Piano Loop`;
                        // let bgm = `bgm/Banjo-Menu-Loop`;
                        bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, bgm, AudioClip).then(res => {
                                SSS_AudioManager.Instance.playBGM(res, true);

                                // const audioId = audioEngine.playMusic(res, true);

                                // // 播放背景音乐并获取音频ID
                                // audioEngine.playMusic(bgmClip, true);

                                // 设置播放速度（Cocos Creator 2.4.0 + 版本支持）
                                // audioEngine.setPlaybackRate(audioId, 0.75); // 0.75倍速
                        });

                        // // let bgm = `bgm/Banjo Theme/Banjo-Gameplay-Loop`;
                        // // let bgm = `bgm/Casual Game Music 11`;
                        // let bgm = `bgm/To Mars And Back_Piano Loop`;
                        // // let bgm = `bgm/Banjo-Menu-Loop`;
                        // bdlMgr.getRes<AudioClip>(SSS_Consts.BundleName.AUDIO, bgm, AudioClip).then(res => {
                        //         const audioId = audioEngine.playMusic(res, true);

                        //         //                 // 播放背景音乐并获取音频ID
                        //         // audioEngine.playMusic(bgmClip, true);

                        //         // 设置播放速度（Cocos Creator 2.4.0+ 版本支持）
                        //         // audioEngine.setPlaybackRate(audioId, 0.75); // 0.75倍速

                        // });
                }
                else {
                        MainPanel.Instance.ShowOn();
                }

                SSS_GameData.isFirstEnterMainScene = false;

                director.preloadScene(SSS_Consts.SceneName.YangLeGeYang_Game_Scene);
        }

        ShowOn() {
                this.node.active = true;
        }
        ShowOff() {
                this.node.active = false;
        }
}